package com.dream.keigezhushou.Activity.acty.play;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.BaseActivity;
import com.dream.keigezhushou.Activity.Uiutils.FullyGridLayoutManager;
import com.dream.keigezhushou.Activity.adapter.GridImageAdapter;
import com.dream.keigezhushou.R;
import com.luck.picture.lib.model.FunctionConfig;
import com.luck.picture.lib.model.LocalMediaLoader;
import com.luck.picture.lib.model.PictureConfig;
import com.yalantis.ucrop.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PublishGameActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_picture)
    RecyclerView rvPicture;
    @BindView(R.id.btn_publish)
    Button btnPublish;

    private int selectMode = FunctionConfig.MODE_MULTIPLE; //图片多选
    private int maxSelectNum = 9;// 图片最大可选数量
    private boolean isShow = true; //是否显示相机
    private int selectType = LocalMediaLoader.TYPE_IMAGE; //添加类型为图片
    private boolean enablePreview = true; //是否预览
    private boolean enableCrop = false; //不可裁剪
    private boolean theme = false;
    private boolean selectImageType = false; //图片是否选中
    private boolean isCompress = false;  //是否压缩
    private boolean isCheckNumMode = false; //是否显示在选择图片的时候带数字
    private int compressFlag = 1;// 1 系统自带压缩 2 luban压缩
    private List<LocalMedia> selectMedia = new ArrayList<>();
    private GridImageAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_game);
        ButterKnife.bind(this);

        initView();
        initData();
    }


    //初始化界面
    protected void initView(){

        tvTitle.setText("发玩法");

        //RecyclerView的初始化
        FullyGridLayoutManager manager = new FullyGridLayoutManager(PublishGameActivity.this, 4, GridLayoutManager.VERTICAL, false);
        rvPicture.setLayoutManager(manager);
        adapter = new GridImageAdapter(this,onAddPicClickListener);
        adapter.setSelectMax(maxSelectNum);
        rvPicture.setAdapter(adapter);

    }

    //初始化数据
    protected void initData(){

        ivReturn.setOnClickListener(this);

        adapter.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {

                //点击预览图片
                PictureConfig.getPictureConfig().externalPicturePreview(PublishGameActivity.this,position,selectMedia);
            }
        });
    }


    /**
     * 删除图片回调接口
     */
    private GridImageAdapter.onAddPicClickListener onAddPicClickListener = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick(int type, int position) {
            switch (type) {
                case 0:
                    // 进入相册

                    int selector = R.drawable.select_cb;
                    FunctionConfig config = new FunctionConfig();
                    config.setType(selectType);
                    config.setType(selectType);
                    config.setCompress(isCompress);
                    config.setEnablePixelCompress(true);  //启用像素压缩
                    config.setEnableQualityCompress(true); //是否启用质量压缩
                    config.setMaxSelectNum(maxSelectNum);
                    config.setSelectMode(selectMode);
                    config.setShowCamera(isShow);
                    config.setEnablePreview(enablePreview);
                    config.setEnableCrop(enableCrop);
                    config.setCheckNumMode(isCheckNumMode);
                    config.setImageSpanCount(4); //每行显示个数
                    config.setSelectMedia(selectMedia); //已选择图片


                    if (theme) {
                        config.setThemeStyle(ContextCompat.getColor(PublishGameActivity.this, R.color.blue));
                        // 可以自定义底部 预览 完成 文字的颜色和背景色
                        if (!isCheckNumMode) {
                            // QQ 风格模式下 这里自己搭配颜色，使用蓝色可能会不好看
                            config.setPreviewColor(ContextCompat.getColor(PublishGameActivity.this, R.color.white));
                            config.setCompleteColor(ContextCompat.getColor(PublishGameActivity.this, R.color.white));
                            config.setPreviewBottomBgColor(ContextCompat.getColor(PublishGameActivity.this, R.color.blue));
                            config.setBottomBgColor(ContextCompat.getColor(PublishGameActivity.this, R.color.blue));
                        }
                    }
                    if (selectImageType) {
                        config.setCheckedBoxDrawable(selector);
                    }

                    // 先初始化参数配置，在启动相册
                    PictureConfig.init(config);
                    PictureConfig.getPictureConfig().openPhoto(PublishGameActivity.this, resultCallback);
                    break;
                case 1:
                    // 删除图片
                    selectMedia.remove(position);
                    adapter.notifyItemRemoved(position);
                    break;
            }
        }
    };

    /**
     * 图片回调方法
     */
    private PictureConfig.OnSelectResultCallback resultCallback = new PictureConfig.OnSelectResultCallback() {
        @Override
        public void onSelectSuccess(List<LocalMedia> resultList) {
            selectMedia = resultList;
            Log.i("callBack_result", selectMedia.size() + "");
            if (selectMedia != null) {
                adapter.setList(selectMedia);
                adapter.notifyDataSetChanged();
            }
        }
    };


    //点击事件的实现

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.iv_return:
                break;

            case R.id.btn_publish:
                break;
        }
    }

}
